<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
			<el-form-item label="领用单号" prop="batchNumber">
				<el-input v-model="queryParams.batchNumber" placeholder="请输入领用单号" clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="领用主题" prop="outTheme">
				<el-input v-model="queryParams.outTheme" placeholder="请输入领用主题" clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="计划领用日期">
				<el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-"
				 start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions"></el-date-picker>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-table v-loading="loading" :data="elecMaterialOutinfoList" @selection-change="handleSelectionChange">
			<el-table-column label="主键ID" align="center" prop="outId" />
			<el-table-column label="领用单号" align="center" prop="batchNumber" />
			<el-table-column label="领用主题" align="center" prop="outTheme" />
			<el-table-column label="计划领用日期" align="center" prop="planOutDate" width="180">
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.planOutDate, '{y}-{m}-{d}') }}</span>
				</template>
			</el-table-column>
			<el-table-column label="领用人" align="center" prop="outUserName" />
			<el-table-column label="领用状态" align="center" prop="outStatus">
				<template slot-scope="scope">
					<dict-tag :options="dict.type.out_status" :value="scope.row.outStatus" />
				</template>
			</el-table-column>
			<el-table-column label="备注" align="center" prop="remark" />
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button size="mini" type="text" icon="el-icon-search" @click="handleUpdate(scope.row)" v-hasPermi="['elec:elecMaterialReturninfo:see']">查看详情</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
		 @pagination="getList" />

		<!-- 添加或修改工器具领用对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="1100px" append-to-body>
			<el-table v-loading="loading" :data="elecMaterialList" height="450">
				<el-table-column label="主键ID" align="center" prop="materialId" />
				<el-table-column label="工器具编码" align="center" prop="materialCode" width="100" />
				<el-table-column label="工器具名称" align="center" prop="materialName" width="200" />
				<el-table-column label="是否出库" align="center" prop="isOut">
					<template slot-scope="scope">
						<dict-tag :options="dict.type.is_out" :value="scope.row.isOut" />
					</template>
				</el-table-column>
				<el-table-column label="是否归还" align="center" prop="isReturn">
					<template slot-scope="scope">
						<dict-tag :options="dict.type.is_return" :value="scope.row.isReturn" />
					</template>
				</el-table-column>
				<el-table-column label="分类" align="center" prop="sortName" width="100" />
				<el-table-column label="库房" align="center" prop="warehouseName" width="100" />
				<el-table-column label="柜架" align="center" prop="cabinetName" width="100" />
				<el-table-column label="层位" align="center" prop="gridName" width="100" />
				<el-table-column label="供应商" align="center" prop="providerName" width="100" />
			</el-table>
			<div slot="footer" class="dialog-footer">
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		listElecMaterialOutinfo,
		getElecMaterialOutinfo,
		delElecMaterialOutinfo,
		addElecMaterialOutinfo,
		updateElecMaterialOutinfo,
		listUserOptions,
		listElecMaterial
	} from "@/api/elec/elecMaterialReturninfo";

	export default {
		name: "ElecMaterialReturninfo",
		dicts: ['out_status', 'is_return', 'is_out'],
		data() {
			return {
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				//日期控件
				dateRange: [],
				// 总条数
				total: 0,
				// 选择工器具列表总条数
				totalMaterial: 0,
				// 工器具领用表格数据
				elecMaterialOutinfoList: [],
				// 工器具表格数据
				elecMaterialList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 弹出层标题（选择）
				selectTitle: "",
				// 是否显示弹出层（选择）
				selectOpen: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					materialCategory: '1',
					batchNumber: null,
					outTheme: null,
					planOutDate: null,
					outUserId: null,
					outStatus: null,
					outStatusStr: ["2", "3", "4"],
				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {
					outTheme: [{
						required: true,
						message: "领用主题不能为空",
						trigger: "blur"
					}],
					planOutDate: [{
						required: true,
						message: "计划领用日期不能为空",
						trigger: "blur"
					}],
					outUserId: [{
						required: true,
						message: "领用人不能为空",
						trigger: "change"
					}],
				},
				userOptions: [],
				pickerOptions: {
					shortcuts: [{
						text: '最近一周',
						onClick(picker) {
							const end = new Date();
							const start = new Date();
							start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
							picker.$emit('pick', [start, end]);
						}
					}, {
						text: '最近一个月',
						onClick(picker) {
							const end = new Date();
							const start = new Date();
							start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
							picker.$emit('pick', [start, end]);
						}
					}, {
						text: '最近三个月',
						onClick(picker) {
							const end = new Date();
							const start = new Date();
							start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
							picker.$emit('pick', [start, end]);
						}
					}]
				},
			};
		},
		created() {
			this.getList();
		},
		methods: {
			/** 查询工器具领用列表 */
			getList() {
				this.loading = true;
				listElecMaterialOutinfo(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
					this.elecMaterialOutinfoList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					outId: null,
					batchNumber: null,
					outTheme: null,
					planOutDate: null,
					outUserId: null,
					materialCategory: null,
					outStatus: null,
					remark: null,
					delFlag: null,
					createBy: null,
					createTime: null,
					updateBy: null,
					updateTime: null,
				};
				this.elecMaterialList = [];
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.dateRange = [];
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.outId)
				this.single = selection.length !== 1
				this.multiple = !selection.length
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				const outId = row.outId || this.ids
				getElecMaterialOutinfo(outId).then(response => {
					this.form = response.data;
					this.elecMaterialList = response.data.elecMaterialList;
					this.open = true;
					this.title = "查看工器具归还";
				});
			},
		}
	};
</script>
